package com.myk.game.gobangchess.expand.bwantispace;

import java.util.ArrayList;
import java.util.List;

/**
     * 棋子-怪物-丘林
     */
public class MonsterQL extends BaseMonsterPiece  implements IGameInformation{
        public MonsterQL(Game game,int row, int col) {
            super(game,"丘林", row, col);
            this.hp = 10;
            this.atk = 5;
        }

        @Override
        public void updateSelf() {
            //玩家在攻击范围内，1/4的几率进行攻击
            if (Math.random() < 0.25 && isAdjacentToPiece(game.human)){
                this.attack(game.human);
                return;
            }
            //玩家在视野范围内，优先朝着玩家移动
            if (isInView(game.human) && !isAdjacentToPiece(game.human)){
                int[] moveDir = getMoveDir(game.human);
                //丘林只能直走1格，如果给定方向是斜走的化，随机一个移动方向变为0
                dirStraightness(moveDir);
                this.move(this.row + moveDir[0],  this.col + moveDir[1]);
                return;
            }
            //暴徒在视野内，优先朝着暴徒移动
            MonsterBT bt = getNearestBtInView();
            if (bt != null && !isAdjacentToPiece(bt)){
                int[] moveDir = getMoveDir(bt);
                dirStraightness(moveDir);
                this.move(this.row + moveDir[0],  this.col + moveDir[1]);
                return;
            }
            //随机游走
            moveRandom();
        }

        //目标棋子在自己的视野范围内
        public boolean isInView(BasePiece piece){
            return game.helper.room.manager.getDistance(this.row,this.col, piece.row, piece.col) <= 4;
        }

        //移动方向直向化
        private static void dirStraightness(int[] moveDir) {
            if (moveDir[0] !=0 && moveDir[1] != 0){
                if (Math.random() > 0.5) {
                    moveDir[0] = 0;
                }
                else {
                    moveDir[1] = 0;
                }
            }
        }

        //获取视野范围内最近的暴徒
        private MonsterBT getNearestBtInView() {
            int vp = 4; //视野距离
            int minDistance = Integer.MAX_VALUE;
            MonsterBT nearestBt = null;
            for (BaseMonsterPiece monster : game.fieldMonsterList) {
                if (monster instanceof MonsterBT){
                    int dis = game.helper.room.manager.getDistance(this.row,this.col, monster.row, monster.col);
                    if (dis <= vp && dis < minDistance){
                        minDistance = dis;
                        nearestBt = (MonsterBT) monster;
                    }
                }
            }
            return nearestBt;
        }


        //根据目标棋子获取移动方向
        public int[] getMoveDir(BasePiece piece) {
            int[] dir = new int[]{0,0};
            if (piece.row > this.row){
                dir[0] = 1;
            }else if (piece.row < this.row){
                dir[0] = -1;
            }
            if (piece.col > this.col){
                dir[1] = 1;
            }else if (piece.col < this.col){
                dir[1] = -1;
            }
            return dir;
        }

        //随机游走
        private void moveRandom(){
            //获取所有相邻位置的空格
            List<int[]> emptyPositions = new ArrayList<>();
            if (this.row != 0) emptyPositions.add(new int[]{this.row-1, this.col});
            if (this.row != game.helper.getRowCount()-1) emptyPositions.add(new int[]{this.row+1, this.col});
            if (this.col != 0) emptyPositions.add(new int[]{this.row, this.col-1});
            if (this.col != game.helper.getColCount()-1) emptyPositions.add(new int[]{this.row, this.col+1});
            if (emptyPositions.size() > 0){
                int[] randomPosition = emptyPositions.get((int) (Math.random() * emptyPositions.size()));
                move(randomPosition[0], randomPosition[1]);
            }
        }

        @Override
        public String getInformation() {
            return "【怪物-丘林】\n" +
                    "生命值：" + this.hp + "\n" +
                    "攻击力：" + this.atk + "\n";
        }
    }